package com.zsk.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.util.Hashtable;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class QRCode extends JFrame {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    JTextField infoTxt;
    JPanel imagePanel;
    JButton genBtn;
    BitMatrix byteMatrix = null;
    boolean drawed = false;

    public void init() {

        setSize(800, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel note = new JLabel("Input your infomation");
        infoTxt = new JTextField();
        genBtn = new JButton("Generate code");
        imagePanel = new CodePanel();
        genBtn.addActionListener(e -> {
            String content = infoTxt.getText();
            if (content == null || "".equals(content)) {
                JOptionPane.showMessageDialog(this, "Please inout your info");
                return;
            } else {
                drawed = true;
            }
            imagePanel.repaint();
        });

        JPanel topPanel = new JPanel(new GridLayout(1, 3));
        topPanel.add(note);
        topPanel.add(infoTxt);
        topPanel.add(genBtn);

        add(topPanel, BorderLayout.NORTH);
        add(imagePanel, BorderLayout.CENTER);

        setVisible(true);

    }

    private void generateCode() {

        int h = imagePanel.getHeight();
        Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        String content = infoTxt.getText();
        int qrCodeSize = h / 2;

        try {
            byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }

    class CodePanel extends JPanel {
        /**
         *
         */
        private static final long serialVersionUID = 1L;

        @Override
        public void paint(Graphics g) {

            super.paint(g);
            if (drawed) {
                generateCode();
            }
            if (byteMatrix == null)
                return;
            int h = imagePanel.getHeight();
            int w = imagePanel.getWidth();
            int matrixWidth = byteMatrix.getWidth();
            // draw background
            g.drawRect((w - h / 2) / 2, (h - h / 2) / 2, h / 2, h / 2);
            g.setColor(Color.WHITE);
            g.fillRect((w - h / 2) / 2, (h - h / 2) / 2, matrixWidth, matrixWidth);

            // draw code
            g.setColor(Color.black);
            for (int i = 0; i < matrixWidth; i++) {
                for (int j = 0; j < matrixWidth; j++) {
                    if (byteMatrix.get(i, j)) {
                        g.fillRect((w - h / 2) / 2 + i, (h - h / 2) / 2 + j, 1, 1);
                    }
                }
            } ;

        }
    }

    public static void main(String[] args) {
        new QRCode().init();
    }
}
